//
//  YHMeSettlementMakeCell.m
//  YHApp
//
//  Created by -- on 2021/7/27.
//

#import "HAMeSettlementMakeCell.h"
#import "HAMeSettlementMo.h"
#import "HAMeSettlementItemMo.h"
#import "HAMeSettlementItemCell.h"

@interface HAMeSettlementMakeCell ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

/***/
@property (nonatomic, strong) UIView *topView;
/***/
@property (nonatomic, strong) UILabel *topTitLabel;
/***/
@property (nonatomic, strong) UILabel *tit1Label;
/***/
@property (nonatomic, strong) UILabel *tit2Label;
/***/
@property (nonatomic, strong) UIView *lineView;
/***/
@property (nonatomic, strong) FL_Button *r1Button;
/***/
@property (nonatomic, strong) UITextField *nameTextField;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HAMeSettlementMakeCell

-(void)mButtonClick {
    if (self.YHMeSettlementMakeCellSelBlock) {
        self.YHMeSettlementMakeCellSelBlock();
    }
}

-(void)setModel:(HAMeSettlementMo *)model {
    _model = model;
    
    self.nameTextField.text = _model.name;
    [self.tableView reloadData];
}

-(void)setCanalModel:(HACanalItemMo *)canalModel {
    _canalModel = canalModel;
    
    if (_canalModel == nil) {
        [_r1Button setTitle:@"请选择产品渠道" forState:UIControlStateNormal];
        [_r1Button setTitleColor:[UIColor colorWithHexString:@"#858b9c"] forState:UIControlStateNormal];
    }else {
        [_r1Button setTitle:_canalModel.text forState:UIControlStateNormal];
        [_r1Button setTitleColor:[UIColor colorWithHexString:@"#111a34"] forState:UIControlStateNormal];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.itemArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54+54+28;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HAMeSettlementItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeSettlementItemCellID"];
    if (!cell) {
        cell = [[HAMeSettlementItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMeSettlementItemCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.itemModel = _model.itemArray[indexPath.row];
    return cell;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.nameTextField) {
        _model.name = textField.text;
    }
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.backgroundColor = [UIColor khaBGColor];
    
    [self.topTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(20);
        make.height.mas_offset(18);
    }];

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(44);
        make.height.mas_offset(54+54);
    }];
        
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_offset(0.8);
        make.centerY.mas_equalTo(self.topView);
    }];

    [self.tit1Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.left.mas_equalTo(20);
        make.width.mas_offset(80);
        make.height.mas_offset(18);
    }];
    
    [self.tit2Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-18);
        make.left.mas_equalTo(20);
        make.width.mas_offset(80);
        make.height.mas_offset(18);
    }];
    
    [self.r1Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(120);
        make.height.mas_offset(18);
        make.centerY.mas_equalTo(self.tit1Label);
        make.right.mas_equalTo(-20);
    }];

    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.tit2Label);
        make.height.mas_offset(32);
        make.left.mas_equalTo(self.tit1Label.mas_right).offset(10);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView ha_createView];
        _topView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_topView];
    }
    return _topView;
}

- (UILabel *)topTitLabel
{
    if (!_topTitLabel) {
        _topTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#858b9c" text:@"设置结算低价" alignment:0];
        [self.contentView addSubview:_topTitLabel];
    }
    return _topTitLabel;
}

- (UILabel *)tit1Label
{
    if (!_tit1Label) {
        _tit1Label = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"产品渠道" alignment:0];
        [self.topView addSubview:_tit1Label];
    }
    return _tit1Label;
}

- (UILabel *)tit2Label
{
    if (!_tit2Label) {
        _tit2Label = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"模板名称" alignment:0];
        [self.topView addSubview:_tit2Label];
    }
    return _tit2Label;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView ha_createView];
        _lineView.backgroundColor = [UIColor kLineColor];
        [self.topView addSubview:_lineView];
    }
    return _lineView;
}

- (FL_Button *)r1Button
{
    if (!_r1Button) {
        _r1Button = [[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusRight];
        [_r1Button setTitle:@"请选择产品渠道" forState:UIControlStateNormal];
        [_r1Button setTitleColor:[UIColor colorWithHexString:@"#858b9c"] forState:UIControlStateNormal];
        _r1Button.titleLabel.font = [UIFont systemFontOfSize:16];
        _r1Button.fl_padding = 8;
        [_r1Button setImage:[UIImage imageNamed:@"ic_cell_more"] forState:UIControlStateNormal];
        [_r1Button addTarget:self action:@selector(mButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:_r1Button];
    }
    return _r1Button;
}

- (UITextField *)nameTextField
{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc]init];
        _nameTextField.delegate = self;
        _nameTextField.textColor = [UIColor colorWithHexString:@"#111a34"];
        _nameTextField.font = [UIFont systemFontOfSize:17];
        _nameTextField.textAlignment = 2;
        _nameTextField.placeholder = @"请输入模板名称";
        [self.topView addSubview:_nameTextField];
    }
    return _nameTextField;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:_tableView];
    }
    return _tableView;
}

@end
